package ensemble.samples.custom.sqlformat.javafx;

import ensemble.samples.custom.sqlformat.javafx.util.MybatisSqlFormat;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * format sql with param
 *
 * @sampleName sql format
 * @preview preview.png
 * @see Button
 * @highlight
 *
 */
public class SqlFormatApp extends Application {
 

    public Parent createContent() {

        TextArea in = new TextArea("");
        in.setFont(Font.font("宋体"));
        in.setPromptText("请输入...");
        ScrollPane intScrollPane = new ScrollPane();
        intScrollPane.setContent(in);
        intScrollPane.setPrefWidth(800); // Workaround of RT-21495
        intScrollPane.setPrefHeight(245);
        intScrollPane.setFitToWidth(true);
        intScrollPane.setFitToHeight(true);

        TextArea out = new TextArea();

        ScrollPane outScrollPane = new ScrollPane();
        outScrollPane.setContent(out);
        outScrollPane.setPrefHeight(245);
        outScrollPane.setFitToWidth(true);
        outScrollPane.setFitToHeight(true);

        EventHandler<ActionEvent> copy2Clipboard = (event) -> {
            // put it to clipboard
            ClipboardContent cc = new ClipboardContent();
            cc.putString(out.getText());
            Clipboard.getSystemClipboard().setContent(cc);
        };

        Button button = new Button("sql格式化");
        button.setOnAction((ActionEvent arg0) ->{
            out.setText(MybatisSqlFormat.getSqlFromStr(in.getText()));
            copy2Clipboard.handle(arg0);
        });
        Button button1 = new Button("增加单引号");
        button1.setOnAction((ActionEvent arg0) -> {
            String format;
            if (in.getText().isEmpty()) {
                format = "";
            } else {
                format = String.format("'%s'", in.getText().trim().replaceAll("\n", "',\n'"));
            }
            out.setText(format);
            copy2Clipboard.handle(arg0);
        });
        HBox hBox = new HBox(button, button1);
        hBox.setAlignment(Pos.CENTER);
        hBox.setSpacing(10);

        Button copyButton = new Button("复制到剪切板");
        copyButton.setOnAction(copy2Clipboard);
        VBox.setVgrow(intScrollPane, Priority.ALWAYS);
        VBox.setMargin(intScrollPane, new Insets(8));
        VBox.setMargin(hBox, new Insets(0, 0, 10, 0));
        VBox.setVgrow(outScrollPane, Priority.ALWAYS);
        VBox.setMargin(outScrollPane, new Insets(8));
        VBox.setMargin(copyButton, new Insets(0, 0, 20, 0));

        VBox vRoot = new VBox();
        vRoot.setAlignment(Pos.CENTER);
        vRoot.setSpacing(5);
        vRoot.getChildren().addAll(intScrollPane, hBox, outScrollPane, copyButton);
 
        return vRoot;
    }
 
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setScene(new Scene(createContent()));
        primaryStage.show();
    }

    /**
     * Java main for when running without JavaFX launcher
     */
    public static void main(String[] args) {
        launch(args);
    }

}